package com.gw.yunke.service.impl;

import com.gw.yunke.dao.CourseMapper;
import com.gw.yunke.dao.PartMapper;
import com.gw.yunke.dao.PartVideoMapper;
import com.gw.yunke.dao.SectionMapper;
import com.gw.yunke.dao.VideoMapper;
import com.gw.yunke.domain.Course;
import com.gw.yunke.domain.Part;
import com.gw.yunke.domain.PartExample;
import com.gw.yunke.domain.PartVideo;
import com.gw.yunke.domain.Section;
import com.gw.yunke.domain.User;
import com.gw.yunke.domain.Video;
import com.gw.yunke.domain.dto.PartReq;
import com.gw.yunke.service.IPartService;
import com.gw.yunke.service.IUserService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PartServiceImpl implements IPartService {

  private static final Logger log = LoggerFactory.getLogger(PartServiceImpl.class);

  @Autowired
  private PartMapper partMapper;
  @Autowired
  private MultipartFileService multipartFileService;
  @Autowired
  private PartVideoMapper partVideoMapper;
  @Autowired
  private SectionMapper sectionMapper;
  @Autowired
  private IUserService userService;
  @Autowired
  private CourseMapper courseMapper;
  @Autowired
  private VideoMapper videoMapper;

  //@Cacheable(value = "parts",key = "sectionId")
  @Override
  public List<Part> getPartsBySectionId(Long sectionId) {

    PartExample partExample = new PartExample();
    PartExample.Criteria criteria = partExample.createCriteria();
    criteria.andSectionIdEqualTo(sectionId);
    partExample.setOrderByClause("part_order");
    return partMapper.selectByExample(partExample);
  }

  //@CacheEvict(value = "parts",key = "#{part.sectionId}")
  @Override
  public int createPart(PartReq partReq, MultipartFile multipartFile, User user) throws Exception {
    Part part = new Part();

    //TODO: videoId检测
    if(partReq.getVideoId()==null || partReq.getVideoId()<0){
      throw new Exception("缺失videoId");
    }
    BeanUtils.copyProperties(partReq,part);
    if(selectPartByExample(part)){
      throw new Exception("不能重复创建同序号小节");
    }
    int result = partMapper.insertSelective(part);
    Section section = sectionMapper.selectByPrimaryKey(part.getSectionId());
    if(!userService.checkCreatePermission(user,section.getCourseId())){
      log.error("没有对当前课程操作的权限 courseId={},userId = {}",section.getCourseId(),user.getId());
      throw new Exception("没有对当前课程操作的权限");
    }
    //if (multipartFile != null)
      //videoId = multipartFileService.upLoadVideo(multipartFile, user,section.getCourseId());
    if(partReq.getVideoId() > 0 && result > 0) {
      Course course = courseMapper.selectByPrimaryKey(section.getCourseId());
      Video video = videoMapper.selectByPrimaryKey(partReq.getVideoId());
      SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss");
      int hours = course.getAllTime().getHours() + video.getLength().getHours();
      int minutes = course.getAllTime().getMinutes() + video.getLength().getMinutes();
      int seconds = course.getAllTime().getSeconds() + video.getLength().getSeconds();
      course.setAllTime(format.parse(hours+":"+minutes+":"+seconds));

      PartVideo partVideo = new PartVideo();
      partVideo.setPartId(part.getId());
      partVideo.setVideoId(partReq.getVideoId());
      partVideo.setCourseId(section.getCourseId());
      return partVideoMapper.insertSelective(partVideo);
    }
    return -1;
  }

  private boolean selectPartByExample(Part part){
    PartExample partExample = new PartExample();
    PartExample.Criteria criteria = partExample.createCriteria();
    if(part.getSectionId() != null){
      criteria.andSectionIdEqualTo(part.getSectionId());
    }
    if(part.getPartOrder() != null){
      criteria.andPartOrderEqualTo(part.getPartOrder());
    }
    List<Part> partList = partMapper.selectByExample(partExample);
    return partList.size() > 0;
  }
}
